package com.iuie.ns.system.controller.menu;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iuie.ns.frame.utils.StringUtils;
import com.iuie.ns.frame.utils.beans.UserOperateVO;
import com.iuie.ns.frame.web.response.vo.ResponseInfo;
import com.iuie.ns.system.service.menu.ISysMenuService;
import com.iuie.ns.system.vo.resource.SysResourceVO;

/**
 * @desc 菜单管理
 * @createTime 2017年6月25日 下午1:02:27
 * @author iuie
 */
@RequestMapping("sys/menu")
@Controller
public class SysMenuController {
    
    @Resource
    private ISysMenuService menuService;
    
    @RequestMapping
    public String index() {
        return "view-system/menu/menu";
    }
    
    /**
     * 供选择菜单的页面
     * @param type 
     * when type = 1: base on current role query nodes, all node allow check
     * when type = 2: base on current role query nodes, only allow leaf node check
     * @param pageKey
     * @param id
     * @return
     * @date 2018年3月25日下午3:07:40
     */
    @RequestMapping("select/{type}/{pageKey}/{id}/index")
    public String selectMenuIndex(@PathVariable("type") String type, @PathVariable("pageKey") String pageKey, @PathVariable("id") String id, 
    		HttpServletRequest request) {
    	request.setAttribute("type", type);
    	request.setAttribute("pageKey", pageKey);
    	if (!StringUtils.equals(id, "-")) {
    		request.setAttribute("id", id);
    	}
        return "view-system/menu/selectMenu";
    }
    
    /**
     * 查询所有菜单
     * @param params
     * @return
     * @throws Exception
     * @date 2018年3月25日下午3:31:17
     */
    @RequestMapping(value="findAll")
    @ResponseBody
    public ResponseInfo findAll(SysResourceVO params) throws Exception{
        List<SysResourceVO> list = menuService.findAll(params);
        ResponseInfo ri = new ResponseInfo(list, "查询成功！");
        return ri;
    }
    
    /**
     * 关联当前登录角色查询菜单
     * @return
     * @date 2018年3月25日下午3:32:18
     */
    @RequestMapping(value="find/currentRole/menus")
    @ResponseBody
    public ResponseInfo findCurrentRoleMenus() {
    	List<SysResourceVO> list = menuService.findCurrentRoleMenus();
    	return ResponseInfo.successInfoByObject(list);
    }

    /**
     * 保存用户的新增/修改/删除菜单
     * @param oper 所有操作，包含新增/修改/删除操作
     * @return
     * @author lj
     * @date 2017年7月2日13:12:34
     */
    @RequestMapping(value="save", method=RequestMethod.POST)
    @ResponseBody
    public ResponseInfo save(@RequestBody UserOperateVO oper) throws Exception{
        menuService.save(oper);
        return ResponseInfo.successInfo("保存成功！");
    }
    
    /**
     * 根据资源ID数组查询资源树状名
     * @param resIdArr
     * @return
     * @throws Exception
     * @date 2018年3月25日下午5:31:16
     */
    @RequestMapping(value="getTreeShapeResourceNames", method=RequestMethod.POST)
    @ResponseBody
    public ResponseInfo getTreeShapeResourceNames(@RequestParam(required=false,value="resIdArr[]") String[] resIdArr) throws Exception{
        List<String> list = menuService.getTreeShapeResourceNames(resIdArr);
        return ResponseInfo.successInfoByObject(list);
    }
    
}
